<template>
  <div class="flex flex-col h-full">
    <SearchContainer @reset="() => formRef.resetFields()" @search="getData()">
      <a-form ref="formRef" class="flex-1" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="zjm" class="w-1/5">
          <a-input v-model:value="formState.zjm" placeholder="病种名称/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex mb-3">
        <a-button type="primary" class="mr-2" @click="onClickAdd">新增</a-button>
        <CompressExport name="国家医保病种" code="1307" :search-params="formState" btn-name="下载" />
        <BaseExportTxt name="国家医保病种" code="000815" :search-params="formState" btn-name="导出" class="ml-2" />
      </div>
      <BaseTable fill column-code="000815" :scroll="{ x: 2760 }" :data-source="tableData" bordered :loading="loading" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-switch v-model:checked="record.status" @change="onSelectSwitch(record)" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickAdd(record)">修改</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @ok="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
// import { post } from '@/utils/request'
// import UploadTxt from '../components/UploadTxt'
import BaseExportTxt from '../components/BaseExport'
import CompressExport from '../components/CompressExport'
import Edit from './Edit.vue'

const formRef = ref(null)
const editOpen = ref(false)
const rowData = ref({})
const formState = ref({ zjm: '' })

const { loading, tableData, getData, pagination } = useTable({
  url: '/insure/getNmiDictDiseTypeList',
  params: formState.value,
  immediate: true,
  formatResult: (data) => {
    const arr = data.map((item) => {
      item.status = item.status === 0 ? true : false
      return item
    })
    return arr
  }
})

const onSelectSwitch = async (row) => {
  const { code, msg } = await post('/insure/doSaveNmiDictDiseType', { status: row.status ? 0 : 1, id: row.id })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}

const onClickAdd = (row) => {
  rowData.value = { ...row }
  editOpen.value = true
}
</script>
<style lang="less" scoped></style>
